package profileEditor;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import GUIMain.GUIFocusManager;

import storage.FieldInfo;
import storage.Rule;
import storage.RuleGroup;

public class RuleEditor {
	private static JDialog dialog;
	private static JLabel editingFieldLabel;
	private static JTextField editingFieldText;
	private static JLabel editingFieldTypeLabel;
	private static JTextField editingFieldTypeText;
	private static JButton cancel;
	private static JButton submit;
	private static JButton applyToAll;
	private static JButton help;
	private static ArrayList<RuleGroup> ruleGroupList;
	private static GridBagConstraints gbc;
	private static boolean initialized = false;
	private static ArrayList<Rule> profileRuleList; 
	private static String fieldName;
	/**
	 * Set up GUI
	 */
	public static void initialize(){
		if(!initialized){
			initialized = true;
			dialog = new JDialog();
			editingFieldLabel = new JLabel("Editing Rules for Field");
			editingFieldText = new JTextField();
			editingFieldText.setEditable(false);
			editingFieldText.setPreferredSize(new Dimension(100,25));
			editingFieldText.setMinimumSize(new Dimension(100,25));
			cancel = new JButton("Cancel");
			submit = new JButton("Submit");
			help = new JButton("Help");
			ruleGroupList = new ArrayList<RuleGroup>();
			dialog.setLayout(new GridBagLayout());
			editingFieldTypeLabel = new JLabel("Field Type: ");
			editingFieldTypeText = new JTextField();
			editingFieldTypeText.setEditable(false);
			editingFieldTypeText.setPreferredSize(new Dimension(100,25));
			editingFieldTypeText.setMinimumSize(new Dimension(100,25));
			gbc = new GridBagConstraints();
			applyToAll = new JButton("Apply To All");
			
			gbc.insets.top = 4;
			gbc.insets.bottom = 4;
			gbc.insets.left = 4;
			gbc.insets.right = 4;
			gbc.gridwidth =1;
			gbc.gridheight = 1;
			
			JPanel headerPanel = new JPanel();
			headerPanel.setLayout(new GridBagLayout());
			gbc.anchor = GridBagConstraints.LINE_START;
			gbc.gridx =0;
			gbc.gridy = 0;
			headerPanel.add(editingFieldLabel,gbc);
			gbc.gridx =1;
			gbc.gridy = 0;
			headerPanel.add(editingFieldText,gbc);
			gbc.gridx = 2;
			headerPanel.add(editingFieldTypeLabel,gbc);
			gbc.gridx = 3;
			headerPanel.add(editingFieldTypeText,gbc);
			
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.gridwidth = 2;
			dialog.add(headerPanel,gbc);

			gbc.anchor = GridBagConstraints.CENTER;
			for(int i=0;i<10;i++){
				//Create a new rulegroup
				RuleGroup ruleGroup = new RuleGroup();
				
				//Add rulegroup element to GUI
				gbc.gridx =0;
				gbc.gridy = i+1;
				dialog.add(ruleGroup,gbc);
				
				if(i>0){ //something is there
					ruleGroupList.get(i-1).addActionListener(ruleGroup);
				}
				//Add to list of rulegroups
				ruleGroupList.add(ruleGroup);
			}
			gbc.gridwidth =1;
			//Hides the window
			ActionListener cancelListener = new ActionListener(){
				public void actionPerformed(ActionEvent e){
					GUIFocusManager.remove();
				}
			};
			cancel.addActionListener(cancelListener);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			gbc.gridx = 0;
			gbc.gridy = 0;
			buttonPanel.add(cancel,gbc);
			//Display a help panel
			help.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					RuleEditorHelp.open();
				}
			});
			//Saves changes
			ActionListener submitListener = new ActionListener(){
				public void actionPerformed(ActionEvent e){
					// Field Correctness verification here
					boolean errorFound = false;
					for(int i=0;i<ruleGroupList.size();i++){
						//for each rulegroup check all fields
						if(ruleGroupList.get(i).subjectFieldCombo.getSelectedIndex()>-1){
							if(ruleGroupList.get(i).valueText.getText().equals("")){
								JOptionPane.showMessageDialog(null,"Please enter a value! (Or put NULL or NOT NULL if you wish to " +
								"test if field is blank or filled");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							try{
								Integer.parseInt(ruleGroupList.get(i).valueText.getText());
							}catch(NumberFormatException excep){
							}
							if(ruleGroupList.get(i).valueText.getText().equals("")){
								JOptionPane.showMessageDialog(null,"Please enter a value! (Or put NULL or NOT NULL if you wish to " +
										"test if field is blank or filled");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							if(ruleGroupList.get(i).equalityCombo.isEnabled()&&
									ruleGroupList.get(i).equalityCombo.getSelectedIndex()<0){
								JOptionPane.showMessageDialog(null,"Please select an equality for comparison with the target field");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							if(ruleGroupList.get(i).targetFieldCombo.isEnabled()&&
									ruleGroupList.get(i).targetFieldCombo.getSelectedIndex()<0){
								JOptionPane.showMessageDialog(null,"Please select a target field or date for comparison");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
						}
					}
					if(!errorFound)	{
						ProfileEditor.updateRuleList(fieldName,ruleGroupList);
						GUIFocusManager.remove();
					}
				}
			};
			submit.addActionListener(submitListener);
			gbc.gridx = 1;
			gbc.gridy = 0;
			buttonPanel.add(submit,gbc);
			gbc.gridx = 2;
			gbc.gridy = 0;
			buttonPanel.add(help,gbc);
			gbc.gridx = 3;
			gbc.gridy = 0;
			applyToAll.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					// Field Correctness verification here
					boolean errorFound = false;
					for(int i=0;i<ruleGroupList.size();i++){
						//for each rulegroup check all fields
						if(ruleGroupList.get(i).subjectFieldCombo.getSelectedIndex()>-1){
							if(ruleGroupList.get(i).valueText.getText().equals("")){
								JOptionPane.showMessageDialog(null,"Please enter a value! (Or put NULL or NOT NULL if you wish to " +
								"test if field is blank or filled");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							try{
								Integer.parseInt(ruleGroupList.get(i).valueText.getText());
							}catch(NumberFormatException excep){
							}
							if(ruleGroupList.get(i).valueText.getText().equals("")){
								JOptionPane.showMessageDialog(null,"Please enter a value! (Or put NULL or NOT NULL if you wish to " +
										"test if field is blank or filled");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							if(ruleGroupList.get(i).equalityCombo.isEnabled()&&
									ruleGroupList.get(i).equalityCombo.getSelectedIndex()<0){
								JOptionPane.showMessageDialog(null,"Please select an equality for comparison with the target field");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
							if(ruleGroupList.get(i).targetFieldCombo.isEnabled()&&
									ruleGroupList.get(i).targetFieldCombo.getSelectedIndex()<0){
								JOptionPane.showMessageDialog(null,"Please select a target field or date for comparison");
								ruleGroupList.get(i).valueText.requestFocus();
								errorFound = true;
								break;
							}
						}
					}
					if(!errorFound)	{
						ProfileEditor.updateToAllRuleList(fieldName,ruleGroupList);
						GUIFocusManager.remove();
					}
				}
				
			});
			buttonPanel.add(applyToAll,gbc);
			gbc.gridwidth =2;
			gbc.gridx =0;
			gbc.gridy =11;
			dialog.add(buttonPanel,gbc);
			gbc.gridwidth = 1;
			GUIFocusManager.setDialogProperties(dialog);
		}
	}
	public static void showRuleEditor(String fieldName1){
		fieldName = fieldName1;
		dialog.setTitle("Editing Rules for Field: " + fieldName);
		editingFieldText.setText(fieldName);
		editingFieldTypeText.setText(FieldInfo.getFieldType(fieldName));
		for(int i=0;i<ruleGroupList.size();i++){
			ruleGroupList.get(i).setEnabled(false);
		}
		ruleGroupList.get(0).setEnabled(true);
		GUIFocusManager.add(dialog);
	}
	public static void showRuleEditor(String fieldName1, ArrayList<Rule> profileRuleList1){
		profileRuleList = profileRuleList1;
		fieldName = fieldName1;
		dialog.setTitle("Editing Rules for Field: " + fieldName);
		editingFieldText.setText(fieldName);
		editingFieldTypeText.setText(FieldInfo.getFieldType(fieldName));
		for(int i=0;i<ruleGroupList.size();i++){
			if(i>=profileRuleList.size()){
				ruleGroupList.get(i).setEnabled(false);
			}else{
				ruleGroupList.get(i).clear();
				ruleGroupList.get(i).groupCombo.setSelectedItem(profileRuleList.get(i).groupNo);
				ruleGroupList.get(i).connectorCombo.setSelectedItem(profileRuleList.get(i).connector);
				ruleGroupList.get(i).subjectFieldCombo.setSelectedItem(profileRuleList.get(i).subjectField);
				ruleGroupList.get(i).valueText.setText(profileRuleList.get(i).value);
				ruleGroupList.get(i).equalityCombo.setSelectedItem(profileRuleList.get(i).equality);
				ruleGroupList.get(i).targetFieldCombo.setSelectedItem(profileRuleList.get(i).targetField);
			}
		}
		GUIFocusManager.add(dialog);
	}
	public static void updateFields() {
		for(int i=0;i<ruleGroupList.size();i++){
			ruleGroupList.get(i).updateRuleGroupFields();
		}
	}

}
